﻿
Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class FrontEnd_Control_TinMuaXe_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindKhoangGia()


        End If
    End Sub
#End Region
#Region "Event for control "
    Protected Sub BindKhoangGia()
        Using data As New VSEntities
            Dim p = (From q In data.TinMuaXeKhoangGias Select q).ToList
            Dim itm As New ListItem("--- Chọn ---", "0")
            With ddlTinmuaxekhoanggiaid
                .DataValueField = "Id"
                .DataTextField = "Title"
                .DataSource = p
                .DataBind()
                .Items.Insert(0, itm)
            End With
        End Using
    End Sub
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim p As New VSModel.TinMuaXe
            Try

                p.TieuDe = txtTitle.Text.Trim()
                p.NoiDung = txtDescription.Text.Trim()
                p.TinMuaXeKhoangGiaId = ddlTinmuaxekhoanggiaid.SelectedValue
                p.StatusId = 2
                p.UserId = Session("UserId")
                p.Creator = Session("UserName")
                p.Created = Now

                Dim u = (From h In data.Users Where h.UserId = p.UserId Select h).FirstOrDefault
                If Not u Is Nothing Then
                    p.LienHe = u.FullName
                    p.SoDienThoai = u.Phone
                    p.DiaChi = u.DiaChi
                    p.TinhThanhId = u.TinhTP
                End If



                data.TinMuaXes.AddObject(p)

                data.SaveChanges()
                If p.Id > 0 Then
                    p.MaTin = "MX" & FormatCode(p.Id.ToString)
                    data.SaveChanges()
                End If
                'Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='quan-ly-tin-mua.html';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
